package tr.edu.yildiz.samplegui.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import tr.edu.yildiz.crystalapp.CrystalActivity;
import tr.edu.yildiz.crystalapp.infra.AndroidResourceManager;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class FlightSearchView extends LinearLayout {

    class FlightListAdapter implements ListAdapter {

    	List<Flight> flightList ;
    	Context context ;
    	
    	public FlightListAdapter(List<Flight> flightList,Context context) {
    		this.flightList = flightList;
    		this.context = context;
		}
    	
		public int getCount() {
			return flightList.size();
		}

		public Object getItem(int position) {
			return flightList.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public int getItemViewType(int position) {
			return 1;
		}

		private TableRow addTableRow(TableLayout tableLayout,String label,String value){
			TableRow tableRow = new TableRow(context);
			TextView textView = new TextView(context);
			textView.setText(label);
			tableRow.addView(textView);
			EditText editText = new EditText(context);
			editText.setText(value);
			editText.setEnabled(false);
			tableRow.addView(editText);
			return tableRow;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			final int index = position;
			TextView textView = new TextView(context);
			textView.setText(flightList.get(position).getFlightCode());
			textView.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					TableLayout view = new TableLayout(context);
					view.addView(addTableRow(view, "Flight Code", flightList.get(index).getFlightCode()));
					view.addView(addTableRow(view, "Departure", flightList.get(index).getDeparture()));
					view.addView(addTableRow(view, "Departure Time", flightList.get(index).getDepartureTime().toString()));
					view.addView(addTableRow(view, "Arrival", flightList.get(index).getDestination()));
					view.addView(addTableRow(view, "Arrival Time", flightList.get(index).getArrivalTime().toString()));
					view.addView(addTableRow(view, "Company", flightList.get(index).getCompany()));
					Button backButton = new Button(context);
					backButton.setText("Back");
					backButton.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							((Activity)FlightSearchView.this.getContext()).setContentView(FlightSearchView.this);
							
						}
					});
					view.addView(backButton);
					((Activity)FlightSearchView.this.getContext()).setContentView(view);
					
				}
			});
			
			return textView;
		}

		public int getViewTypeCount() {
			return 1;
		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isEmpty() {
			return false;
		}

		public void registerDataSetObserver(DataSetObserver observer) {
			
		}

		public void unregisterDataSetObserver(DataSetObserver observer) {
			
		}

		public boolean areAllItemsEnabled() {
			return false;
		}

		public boolean isEnabled(int position) {
			return false;
		}
    	
    }
	
	public FlightSearchView(Context context) {
		super(context);
		setOrientation(LinearLayout.VERTICAL);
		
		final EditText editText = new EditText(context);
		Button findButton = new Button(context);
		findButton.setText("Search");
		
		final ListView resultList = new ListView(context);
		resultList.setAdapter(new FlightListAdapter(new ArrayList<Flight>(), getContext()));
		findButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				List<Flight> flights = getFlightList();
				List<Flight> retFlights = new ArrayList<Flight>();
				for (Flight flight : flights) {
					if (flight.getFlightCode().contains(editText.getText())){
						retFlights.add(flight);
					}
				}
				resultList.setAdapter(new FlightListAdapter(retFlights, getContext()));
			}
		});
		
		
		addView(editText);
		addView(findButton);
		addView(resultList);
	
	}
	
	private List<Flight> getFlightList(){
		List<Flight> flightList = new ArrayList<Flight>();
		flightList.add(createFlight("T121", new Date(), new Date(),"Istanbul","Izmir", "THY", "21"));
		flightList.add(createFlight("T122", new Date(), new Date(),"Istanbul","Ankara", "THY", "24"));
		flightList.add(createFlight("T123", new Date(), new Date(),"Istanbul","Frankfurt", "THY", "25"));
		flightList.add(createFlight("P121", new Date(), new Date(),"Istanbul","Ankara","PGS", "12"));
		flightList.add(createFlight("P121", new Date(), new Date(),"Istanbul","Izmir", "PGS", "13"));
		return flightList;
		
	}
	
	private Flight createFlight(String code,Date depTime,Date arrTime,String departure,String destination, String company,String gate){
		Flight flight = new Flight();
		flight.setFlightCode(code);
		flight.setDepartureTime(depTime);
		flight.setArrivalTime(arrTime);
		flight.setCompany(company);
		flight.setGate(gate);
		flight.setDestination(destination);
		flight.setDeparture(departure);
		return flight;
	}

	private AndroidResourceManager getResourceManager(){
		return ((CrystalActivity)FlightSearchView.this.getContext()).getAndroidResourceManager();
	}
	
}
